home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / unix-emulation-src / unix-glue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-30  |  7.2 KB  |  326 lines  |  [TEXT/EMAC]

  1. /*
  2.  * Copyright (C) 1993, 1994 Marc Parmet.
  3.  * This file is part of the Macintosh port of GNU Emacs.
  4.  *
  5.  * GNU Emacs is distributed in the hope that it will be useful,
  6.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  8.  * GNU General Public License for more details.
  9.  */
  10.  
  11. #if defined(THINK_C)
  12. #include <MacHeaders>
  13. #else
  14. #include <Types.h>
  15. #include <Memory.h>
  16. #include <Quickdraw.h>
  17. #include <Windows.h>
  18. #include <TextEdit.h>
  19. #include <Resources.h>
  20. #endif
  21.  
  22. #include <GestaltEqu.h>
  23. #include <Aliases.h>
  24. #include <stdarg.h>
  25. #include "utime.h"
  26. #include "errno.h"
  27. #include "stdio.h"
  28. #include "sys/param.h"
  29. #include "sys/stat.h"
  30. #include "pwd.h"
  31. #include "grp.h"
  32. #include "unix-constants.h"
  33. #include "unix-types.h"
  34.  
  35. typedef int (*funcptr)();
  36.  
  37. int errno;
  38. char **environ;
  39. char *sys_errlist[NERR];
  40. int sys_nerr = NERR;
  41.  
  42. #define define_unix_trap0(RETTYPE,CNAME) \
  43. RETTYPE CNAME(void) { \
  44. return (RETTYPE) unix_trap(unix_##CNAME); }
  45.  
  46. #define define_unix_trap1(RETTYPE,CNAME,ARG1TYPE) \
  47. RETTYPE CNAME(ARG1TYPE a) { \
  48. return (RETTYPE) unix_trap(unix_##CNAME,a); }
  49.  
  50. #define define_unix_trap2(RETTYPE,CNAME,ARG1TYPE,ARG2TYPE) \
  51. RETTYPE CNAME(ARG1TYPE a,ARG2TYPE b) { \
  52. return (RETTYPE) unix_trap(unix_##CNAME,a,b); }
  53.  
  54. #define define_unix_trap3(RETTYPE,CNAME,ARG1TYPE,ARG2TYPE,ARG3TYPE) \
  55. RETTYPE CNAME(ARG1TYPE a,ARG2TYPE b,ARG3TYPE c) { \
  56. return (RETTYPE) unix_trap(unix_##CNAME,a,b,c); }
  57.  
  58. static int
  59. unix_trap(int request,...)
  60. {
  61.     int err;
  62.     volatile int result;
  63.     static int (*unix_entry)(int,int *,va_list);
  64.     static char inited;
  65.     va_list arglist;
  66.     jmp_buf jbuf;
  67.     
  68.     if (!inited) {
  69.         sys_errlist[0] = "no error";
  70.         sys_errlist[EUNDOC] = "undocumented error";
  71.         sys_errlist[ENOTDIR] = "no such folder or volume";
  72.         sys_errlist[ERANGE] = "argument out of range";
  73.         sys_errlist[EXDEV] = "EXDEV error";
  74.         sys_errlist[EINTR] = "EINTR error";
  75.         sys_errlist[ENOENT] = "no such file";
  76.         sys_errlist[EACCES] = "access denied";
  77.         sys_errlist[EBADF] = "bad file name";
  78.         sys_errlist[EEXIST] = "file exists";
  79.         sys_errlist[EEOF] = "end of file";
  80.         sys_errlist[EDIRNE] = "directory not empty";
  81.         sys_errlist[EPERM] = "permission denied";
  82.         sys_errlist[EINVAL] = "invalid argument";
  83.  
  84.         err = Gestalt('uniX',(long *)&unix_entry);
  85.         if (err) ExitToShell();
  86.  
  87.         inited = 1;
  88.     }
  89.     
  90.     /* If this code is PPC based, then unix_entry will be a UPP.
  91.        Otherwise we are 68k based, and unix_entry is either a UPP or ProcPtr. */
  92.     va_start(arglist,request);
  93. #if defined(powerc)
  94.     result = CallUniversalProc((UniversalProcPtr)unix_entry,unix_entry_ProcInfo,
  95.                                request,&errno,arglist);
  96. #else
  97.     if (*(unsigned short *)unix_entry == _MixedModeMagic) {
  98.         /* Mode switch will clobber a4? */
  99.         if (setjmp(jbuf) == 0) {
  100.             result = (*unix_entry)(request,&errno,arglist);
  101.             longjmp(jbuf,1);
  102.         }
  103.     }
  104.     else
  105.         result = (*unix_entry)(request,&errno,arglist);
  106. #endif
  107.     va_end(arglist);
  108.     return result;
  109. }
  110.  
  111. define_unix_trap1(int,        close,        int)
  112. define_unix_trap2(int,        open,        char *,int)
  113. define_unix_trap3(int,        write,        int,char *,int)
  114. define_unix_trap3(int,        read,        int,char *,int)
  115. define_unix_trap1(int,        dup,        int)
  116. define_unix_trap2(int,        creat,        char *,int)
  117. define_unix_trap1(int,        unlink,        char *)
  118. define_unix_trap3(int,        lseek,        int,int,int)
  119. define_unix_trap1(int,        pipe,        int *)
  120. define_unix_trap2(int,        dup2,        int,int)
  121. define_unix_trap2(int,        execvp,        char *,char **)
  122. define_unix_trap1(int,        exit,        int)
  123. define_unix_trap1(int,        _exit,        int)
  124. define_unix_trap2(funcptr,    signal,        int,funcptr)
  125. define_unix_trap1(int,        raise,        int)
  126. define_unix_trap2(int,        kill,        int,int)
  127. define_unix_trap3(int,        ioctl,        int,int,int *)
  128. define_unix_trap1(int,        wait,        int *)
  129. define_unix_trap3(int,        wait3,        int *,int,int)
  130. define_unix_trap1(int,        alarm,        int)
  131. define_unix_trap0(int,        pause)
  132. define_unix_trap1(char *,    getwd,        char *)
  133. define_unix_trap1(int,        chdir,        char *)
  134. define_unix_trap1(int,        sleep,        int)
  135. define_unix_trap0(int,        getpid)
  136. define_unix_trap0(int,        getppid)
  137. define_unix_trap1(int,        fflush,        FILE *)
  138. define_unix_trap1(int,        fclose,        FILE *)
  139. define_unix_trap2(int,        mkdir,        char *,int)
  140. define_unix_trap1(int,        rmdir,        char *)
  141. define_unix_trap2(int,        utime,        char *,struct utimbuf *)
  142. define_unix_trap2(int,        access,        char *,int)
  143. define_unix_trap2(int,        rename,        char *,char *)
  144. define_unix_trap1(int,        isatty,        int)
  145. define_unix_trap2(int,        chmod,        char *,int)
  146. define_unix_trap2(int,        chgrp,        char *,int)
  147. define_unix_trap2(int,        chown,        char *,int)
  148. define_unix_trap2(int,        stat,        char *,struct stat *)
  149. define_unix_trap2(int,        stat_spec,    FSSpec *,struct stat *)
  150. define_unix_trap1(int,        closedir,    struct DIR *)
  151. define_unix_trap1(char *,    getenv,        char *)
  152. define_unix_trap3(int,        readlink,    char *,char *,int)
  153. define_unix_trap2(int,        lstat,        char *,struct stat *)
  154. define_unix_trap2(int,        symlink,    char *,char *)
  155. define_unix_trap1(FILE *,    _iob,        int)
  156. define_unix_trap1(struct DIR *,opendir,    char *)
  157. define_unix_trap1(struct direct *,readdir,struct DIR *)
  158. define_unix_trap2(int,        FSSpec2unixfn,FSSpec *,Handle *)
  159. define_unix_trap3(int,        unixfn2FSSpec,char *,FSSpec *,int)
  160. define_unix_trap0(jmp_buf *,vfork1)
  161. define_unix_trap1(int,        vfork2,        int)
  162.  
  163. void
  164. unix_nop(void)
  165. {
  166.     static char x;
  167.     if ((++x & 31) == 0) unix_trap(unix_unix_nop,0L);
  168. }
  169.  
  170. void
  171. abort(void)
  172. {
  173.     raise(SIGABRT);
  174.     _exit(0);
  175. }
  176.  
  177. int
  178. link(char *s,char *t)
  179. {
  180.     errno = EUNDOC;
  181.     return -1;
  182. }
  183.  
  184. int
  185. mktemp(char *s)
  186. {
  187.     static int n = 0;
  188.     char *start,*end;
  189.  
  190.     start = s;
  191.     while (1) {
  192.         if (*start == 0) return -1;
  193.         if (*start == 'X') break;
  194.         ++start;
  195.     }
  196.  
  197.     end = start;
  198.     while (1) {
  199.         if (*end != 'X') break;
  200.         ++end;
  201.     }
  202.  
  203.     sprintf(start,"%0*d",end-start,n++);
  204.     return 0;
  205. }
  206.  
  207. int
  208. tell(int fd)
  209. {
  210.     return lseek(fd,0,1);
  211. }
  212.  
  213. int
  214. getuid()
  215. {
  216.     return 0;
  217. }
  218.  
  219. int
  220. geteuid()
  221. {
  222.     return 0;
  223. }
  224.  
  225. int
  226. getegid()
  227. {
  228.     return 0;
  229. }
  230.  
  231. static struct passwd pw;
  232. static struct group gp;
  233. static char dir[MAXPATHLEN],name[MAXNAMLEN];
  234. static char unknown[] = "unknown";
  235.  
  236. struct passwd *
  237. getpwuid(uid_t uid)
  238. {
  239.     char **u;
  240.     short err;
  241.     FSSpec spec;
  242.     int too_long;
  243.     
  244.     pw.pw_uid = uid;
  245.     if (uid == 0) {
  246.         // Home directory is expansion of ~/
  247.         err = unixfn2FSSpec("~/",&spec,0);
  248.         if (err) return 0L;
  249.         err = FSSpec2unixfn(&spec,&u);
  250.         if (err) return 0L;
  251.         HLock(u);
  252.         too_long = (strlen(*u) >= MAXPATHLEN);
  253.         if (too_long) { DisposHandle(u); return 0L; }
  254.         strcpy(dir,*u);
  255.         DisposHandle(u);
  256.         pw.pw_dir = dir;
  257.  
  258.         // Name is in System, maybe.
  259.         u = GetResource('STR ',-16096);
  260.         if (u == 0L)
  261.             pw.pw_name = unknown;
  262.         else {
  263.             HLock(u);
  264.             pstrcpy(name,*u);
  265.             HUnlock(u);
  266.             PtoCstr((unsigned char *)name);
  267.             pw.pw_name = name;
  268.         }
  269.     }
  270.     else {
  271.         pw.pw_dir = unknown;
  272.         *(uid_t *)name = uid;
  273.         name[sizeof(uid_t)] = '\0';
  274.         pw.pw_name = name;
  275.     }
  276.  
  277.     return &pw;
  278. }
  279.  
  280. struct passwd *
  281. getpwnam(char *usr)
  282. {
  283.     char **u;
  284.     short err;
  285.     FSSpec spec;
  286.  
  287.     strcpy(dir,"~");
  288.     strcat(dir,usr);
  289.     err = unixfn2FSSpec(dir,&spec,0);
  290.     if (err) return 0L;
  291.     err = FSSpec2unixfn(&spec,&u);
  292.     if (err) return 0L;
  293.     HLock(u);
  294.     if (strlen(*u) >= MAXPATHLEN) { DisposHandle(u); return 0L; }
  295.     strcpy(dir,*u);
  296.     DisposHandle(u);
  297.  
  298.     pw.pw_dir = dir;
  299.     pw.pw_name = unknown;
  300.     pw.pw_uid = 0;
  301.     
  302.     return &pw;
  303. }
  304.  
  305. struct group *
  306. getgrgid(gid_t gid)
  307. {
  308.     gp.gr_gid = gid;
  309.     gp.gr_name = name;
  310.     *(gid_t *)name = gp.gr_gid;
  311.     name[sizeof(gid_t)] = '\0';
  312.     return &gp;
  313. }
  314.  
  315. struct group *
  316. getgrnam(char *name)
  317. {
  318.     return 0L;
  319. }
  320.  
  321. int
  322. umask()
  323. {
  324.     return 0;
  325. }
  326.